<?php


include('db.php');


$where['zt'] = 0;

 


$task =  Db::table('newpos_data')->where($where)->select();


foreach ($task as $k=>$v){
    
    if($v['stime'] <= time()){
        
        
    } else {
        
        unset($task[$k]);
    }
}




 


foreach ($task as $k=>$v){
    
 
 
    
    $wheres['id'] = $v['goods_id'];  //$v['goods_id'];
    $goods_info = Db::table('products')->where($wheres)->find();
    
    $gwheres['id'] = $goods_info['original_id'];
    $ygoods_info =  Db::table('products')->where($gwheres)->find();
    
    
    
    
    $orders['user_id'] = $v['user_id'];
    $orders['seller_id'] =  $v['seller_id'];
    $orders['shipping_address'] = $v['user_addr'];
    $orders['pickup_point_id'] = 0;
    $orders['delivery_status'] = 'pending';
    $orders['payment_type'] = 'offline_payment';
    $orders['manual_payment'] = 1;
    $orders['manual_payment_data'] = $v['user_addr'];
    $orders['payment_status'] =  'paid';
    $orders['payment_details'] =  'offline_payment';
    $orders['grand_total'] =  $goods_info['unit_price'];
    $orders['product_storehouse_total'] =  $ygoods_info['unit_price'];
    
    $orders['product_storehouse_status'] =  0;
    $orders['coupon_discount'] =0.00 ;
    $orders['code'] = date('Ymd').'-'.rand(00000,99999) ;
    $orders['date'] = time();
    $orders['viewed'] = 0;
    $orders['delivery_viewed'] = 0;
    $orders['payment_status_viewed'] = 0;
    $orders['commission_calculated'] = 1;
    //$orders['created_at'] = 0;
   // $orders['updated_at'] = 0;
    $orders['picking_switch'] = 1;
    $orders['shipping_type'] = 1;
    $orders['is_virtually'] = 0;
    $orders['express_info'] = '';
    
    
 
    
    
    $res = Db::table('orders')->add($orders);
    
    
    
    
    
    
  
    
    
    
    
    
    
 
    $or['seller_id'] = $v['seller_id'];
    
    $or['order_id '] = $res;
    
    $or['product_id'] = $v['goods_id'];
    $or['is_storehouse_product'] = 1;
    
    $or['original_product_id'] = $goods_info['original_id'];
    $or['price'] = $goods_info['unit_price'];
    
    $or['original_product_price']= $goods_info['unit_price'];
    $or['original_product_price'] = $ygoods_info['unit_price'];
 
    
    $or['tax'] = 0.00;
    $or['shipping_cost'] = 0.00;
    $or['quantity'] = 1;
    $or['payment_status'] = 'paid';
    $or['delivery_status'] ='pending';
 
 
    $or['created_at'] = date('Y-m-d H:i:s');
    $or['updated_at'] = date('Y-m-d H:i:s');
    
    $res = Db::table('order_details')->add($or);
    
    
    Db::table('newpos_data')->where('id',$v['id'])->update(['zt'=>1]);
    
    $w['id'] =  $v['pos_id'];
    
    $dpos  = Db::table('newpos')->where($w)->find();
    
    $up['snum'] = $dpos['snum']+ 1;
     
    Db::table('newpos')->where('id',$v['pos_id'])->update($up);
    
    
 
   
     
    //
}
?>